package com.haiheng.module_main.mode

import android.annotation.SuppressLint
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.haiheng.library.log.LogUtils
import com.haiheng.library.log.LogWriter
import com.haiheng.library_base.db.BookInventoryInfo
import com.haiheng.library_base.db.KVDatabase
import com.haiheng.library_base.http.api
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.utils.DateUtils
import com.haiheng.library_base.db.BookInfoBean
import com.haiheng.library_base.db.KV
import com.haiheng.module_main.utils.ExcelUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Date

/**
 *
 * @Des: 资产盘点
 * @Author: hwz
 * @Date:2023/6/27 0027
 * @Version: 1.0
 */
class AssetInventoryViewModel(private val bookInfoViewModel: BookInfoViewModel) :BaseViewModel() {

    var areadyInventoryNum = MutableLiveData<Int>()


    /**
     * 添加盘点记录到盘点表
     *
     */
    fun addInventoryRecord(){
        api {
            bookInfoViewModel.bookListData.value?.forEach {
                var b  = BookInventoryInfo()
                b.barCode = it.barCode
                b.bookTitle = it.bookTitle
                b.bookOnShelfStatus = it.bookOnShelfStatus
                b.currentContainerName = it.currentContainerSub?.name
                b.invertoryInfoStatus = 1
                var result = KVDatabase.getDb().getBookInventoryInfoDao().insert(b)
                LogUtils.e("插入盘点记录 barCode：${it.barCode}  resul:${result}")
            }
            getToltalNum()
        }
    }

    fun clearRecord(){
        api {
            var bookInfoList = KVDatabase.getDb().getBookInventoryInfoDao().deleteAll()
            getToltalNum()
        }
    }

    fun addBook(bookInfoBean: BookInfoBean){
        api {
            bookInfoViewModel.addBook(bookInfoBean)
        }
    }

    /**
     * 查询盘点表中是否已经盘点已经盘点过
     * 没有盘过再去查询书籍信息添加
     *
     * @param barCode
     */
    fun isAreadyInventory(barCode:String){
        api {
            var b =  KVDatabase.getDb().getBookInventoryInfoDao().queryByBarCode(barCode)
            if (b == null){//在盘点表没有盘点过的，算是没有盘点过
                bookInfoViewModel.searchBookInfoByBarCode(barCode)
            }
        }
    }

    fun getToltalNum(){
        api {
         var list= KVDatabase.getDb().getBookInventoryInfoDao().queryInfo()
          LogUtils.d("查询到已经盘点的总数：${list.size}")
          areadyInventoryNum.postValue(list.size)
        }
    }


    fun onExportExcel(){
        viewModelScope.launch(Dispatchers.IO) {
            try {
                var dao = KVDatabase.getDb().getBookInventoryInfoDao()
                var list = dao.queryInfo()
                var fileName  = DateUtils.formatDateTime(Date(),DateUtils.DATE_FORMAT_1)
                LogWriter.e("导出盘点记录:${fileName}")
                ExcelUtils.writeExcel(fileName,list)
                //导出完成后清除盘点表
//                dao.deleteAll()
                bookInfoViewModel.showMsg.postValue("导出成功")
            }catch (ex:Exception){
                bookInfoViewModel.showMsg.postValue("导出异常：${ex}")
            }

        }
    }
}